﻿using HuaCheng.DataAccess;
using HuaCheng.Model;
using System.Collections.Generic;
using System.Drawing;

namespace HuaCheng.Controller
{
    public class UcChannelsControl
    {
        //保存序列化文件锁对象
        public static object saveObj = new object();
        /// <summary>
        /// 设置默认通道参数
        /// </summary>
        /// <param name="devId">设备号</param>
        /// <param name="mapCount">每排通道数目</param>
        /// <returns></returns>
        public static List<Channel> GetDefaultChannels(string ip, int mapCount,Color c,int mid,int gid)
        {
            List<Channel> defChls = new List<Channel>();
            int n = mapCount == 0 ? 8 : mapCount;
            for (int i = 0; i < 8; i++)
            {
                Channel chl = new Channel()
                {
                    DevIp = ip,
                    Mid = mid,
                    Gid = gid,
                    Cid = i+1,
                    RealId = i,
                    ChId = mid + "-" + (i+1),
                    StepId = 0,
                    IterId = "",
                    StepTime = "",
                    Voltage = 0.0f,
                    Current = 0.0f,
                    Capacity = 0.0f,
                    Energy = 0.0f,
                    StopStat = false,
                    LockedStat = false,
                    RunStat = false,
                    Batcode = "",
                    CurBatcode = "",
                    ucColor = c,
                    ChlState = Channel.runningState.Finish,
                    ChlStepName = "完成",
                    Title = "",
                    auxChannel = new List<AuxChannel>()
                };
                defChls.Add(chl);
            }
            return defChls;
        }
        /// <summary>
        /// 序列化所有界面通道集合
        /// </summary>
        /// <returns></returns>
        public static List<ChannelList> GetDeserializedChannels()
        {
            List<ChannelList> chls = new List<ChannelList>();
            string path = DicHelper.RetrieveChlInfoPath();
            if (System.IO.File.Exists(path))
            {
                chls = UcChannelsHelper.DeserializedChannels(path);//反序列化通道参数
            }
            return chls;
        }
        /// <summary>
        /// 序列化保存通道参数
        /// </summary>
        /// <param name="chls"></param>
        public static void SaveSerialziedChannel(List<ChannelList> chls)
        {
            lock (saveObj)
            {
                string path = DicHelper.RetrieveChlInfoPath();
                UcChannelsHelper.SerialziedChannel(chls, path);
            }
        }
        public static void SaveRunningChannels(List<ChannelList> chls)
        {
            string path = DicHelper.RetrieveDysChlInfoPath();
            UcChannelsHelper.SerialziedChannel(chls, path);
        }
    }
}
